<%@ page language="java"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://acegisecurity.org/authz" prefix="authz"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytags"%>

<script type="text/javascript">

  function changeContact(i) {
    selected=i.options[i.selectedIndex].value;
    location.href='<c:url value="/private/contactForm.html"/>?id=' + selected
  }

  function move(from, to){
    for(i = from.options.length - 1; i >= 0 ; i--) {
      if(from[i].selected) {
        addOption(to, from[i]);
        removeOption(from, i);
      }
    }
  }

  function addOption(to, opt) {
    var optn = document.createElement("OPTION");
    optn.text = opt.text;
    optn.value = opt.value;
    to.options.add(optn);
  }

  function removeOption(selectbox) {
    selectbox.remove(i);
  }

  function placeInHidden(fromId, hiddenId) {
    var selObj = document.getElementById(fromId);
    var hideObj = document.getElementById(hiddenId);
    hideObj.value = '';
    for (var i=0; i<selObj.options.length; i++) {
      hideObj.value = (hideObj.value ==  '') ? selObj.options[i].value : hideObj.value + ',' + selObj.options[i].value;
    }
  }
    
  function dosubmit() {
  	placeInHidden('makes_l','makesAlert');
  	placeInHidden('sites_l','sitesAlert');
  }  
</script>


<h2>Contacts</h2>

<select name="id" onchange="changeContact(this);">
	<option value="-1">-- New Contact --</option>
	<c:forEach items="${cache.contactList}" var="contact">
		<option value="${contact.id}"
			<c:if test="${command.id == contact.id}">selected="true"</c:if>>${contact.lastName}
		${contact.firstName}</option>
	</c:forEach>
</select>
<form:form action="contactForm.html" name="contactForm"
	onsubmit="dosubmit()">
	<form:hidden path="id" />
	<input type="hidden" id="makesAlert" name="makesAlert" />
	<input type="hidden" id="sitesAlert" name="sitesAlert" />
	<table cellspacing="5">
		<form:hidden path="published" />
		<tr>
			<td><span class="attribute">Last name*: </span></td>
			<td colspan="2"><form:input cssClass="inputLarge"
				cssErrorClass="error inputLarge" path="lastName" /></td>
		</tr>
		<tr>
			<td><span class="attribute">First name: </span></td>
			<td colspan="2"><form:input cssClass="inputLarge"
				cssErrorClass="error inputLarge" path="firstName" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Mail*: </span></td>
			<td colspan="2"><form:input cssClass="inputLarge"
				cssErrorClass="error inputLarge" path="email" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Company: </span></td>
			<td colspan="2"><form:input cssClass="inputLarge"
				cssErrorClass="error inputLarge" path="company" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Web site: </span></td>
			<td colspan="2"><form:input cssClass="inputLarge"
				cssErrorClass="error inputLarge" path="webSite" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Address: </span></td>
			<td colspan="2"><form:textarea path="address" cols="43" rows="5" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Phone: </span></td>
			<td colspan="2"><form:input cssClass="inputLarge"
				cssErrorClass="error inputLarge" path="phone" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Mobile: </span></td>
			<td colspan="2"><form:input cssClass="inputLarge"
				cssErrorClass="error inputLarge" path="mobile" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Fax: </span></td>
			<td colspan="2"><form:input cssClass="inputLarge"
				cssErrorClass="error inputLarge" path="fax" /></td>
		</tr>
		<tr>
			<td><span class="attribute">Comment: </span></td>
		</tr>
		<tr>
			<td colspan="3"><form:textarea path="comment" cols="50"
				rows="10" /></td>
		</tr>
	</table>
	<table cellspacing="5" width="100%">
		<tr>
			<td colspan="4"><span class="attribute">This contact will
			receive alerts for these makes: </span>:</td>
		</tr>
		<tr>
			<td align="center"><select id="makes_l" name="makes_l" size="15"
				multiple="multiple" style="width: 200">
				<c:forEach items="${makes_l}" var="make">
					<option value="${make.id}">${make.name}</option>
				</c:forEach>
			</select></td>
			<td align="center"><input type="button"
				onclick="move(document.contactForm.makes_r, document.contactForm.makes_l);"
				value="&lt" />
			<p><input type="button"
				onclick="move(document.contactForm.makes_l, document.contactForm.makes_r);"
				value="&gt" />
			</td>
			<td align="center"><select id="makes_r" name="makes_r" size="15"
				multiple="multiple" style="width: 200">
				<c:forEach items="${makes_r}" var="make">
					<option value="${make.id}">${make.name}</option>
				</c:forEach>
			</select></td>
		</tr>
		<tr>
			<td>
			<p>
			</td>
		</tr>
	</table>

	<table cellspacing="5" width="100%">
		<tr>
			<td colspan="4"><span class="attribute">No alert will be
			sent for cars coming from these sites: </span>:</td>
		</tr>
		<tr>
			<td align="center"><select id="sites_l" name="sites_l" size="15"
				multiple="multiple" style="width: 200">
				<c:forEach items="${sites_l}" var="site">
					<option value="${site.id}">${site.name}</option>
				</c:forEach>
			</select></td>
			<td align="center"><input type="button"
				onclick="move(document.contactForm.sites_r, document.contactForm.sites_l);"
				value="&lt" />
			<p><input type="button"
				onclick="move(document.contactForm.sites_l, document.contactForm.sites_r);"
				value="&gt" />
			</td>
			<td align="center"><select id="sites_r" name="sites_r" size="15"
				multiple="multiple" style="width: 200">
				<c:forEach items="${sites_r}" var="site">
					<option value="${site.id}">${site.name}</option>
				</c:forEach>
			</select></td>
		</tr>
		<tr>
			<td>
			<p>
			</td>
		</tr>
		<tr>
			<td><input type="submit" name="action" value="save" /> <c:if
				test="${not empty command.id}">
				<input type="submit" name="action" value="delete" />
			</c:if></td>
		</tr>
	</table>
</form:form>
